package com.pesking.core.sqlparser.processor;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;

import com.pesking.core.parameter.Parameter;
import com.pesking.core.sqlparser.parameter.SqlParameter;
import com.pesking.core.sqlparser.parameter.SqlParameterProcessor;

public class IntegerSqlParameterHandler extends SqlParameterProcessor {
	@Override
	public String getTitle() {
		return "整形参数";
	}

	@Override
	public String getDescription() {
		return this.getTitle();
	}

	@Override
	public void handle(StringBuilder sbSQL, Parameter parameter, List<SqlParameter> processors,SqlParameter sqlParameter) {
		sbSQL.append("?");
		processors.add(sqlParameter);
	}

	@Override
	public void setParameter(PreparedStatement ps, Parameter parameter, int index,SqlParameter sqlParameter) throws SQLException {
		String value = parameter.getParameter(sqlParameter.getName());
		if (value != null && value.trim().length() > 0 && !"null".equals(value))
		{
			Double dbl = Double.valueOf(value);
			Integer intValue = dbl.intValue();
			ps.setInt(index, intValue.intValue());
		}
		else
		{
			ps.setNull(index, java.sql.Types.INTEGER);
		}
	}

	@Override
	public List<String> getSqlParameterType() {
		return Arrays.asList(new String []{"N","INT","INTEGER"});
	}
}
